package com.example.utils;

import com.example.constants.PythonConstants;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 张比特
 * @Data: 2024/12/1 下午8:58
 * @Version: v1.0.0
 * @Description: TODO
 **/
public class ScriptUtil {

    /**
     * 封装py路径
     *
     * @param scriptName
     * @return
     */
    public static String getPythonPath(String scriptName) {
        return  PythonConstants.PYTHON_PATH + scriptName + ".py";
    }

    public static String getModelPath(String modelName) {
        return  PythonConstants.MODEL_PATH + modelName + ".pkl";
    }

    /**
     * 处理nii文件
     *
     * @param scriptName
     * @param niiPath
     * @param outputPath
     * @return
     */
    public static int executePythonScript(String scriptName, String niiPath, String outputPath) {
        List<String> command = new ArrayList<>();
        command.add("python.exe");
        command.add(getPythonPath(scriptName)); // 脚本名
        command.add(niiPath); // nii文件路径
        command.add(outputPath); // 输出路径

        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true); // 错误数据标准输出
        Process process = null;
        try {
            process = builder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python脚本退出代码" + exitCode);
            }
            return exitCode;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException("执行失败", e);
        } finally {
            if (process != null && process.isAlive()) {
                process.destroy(); // 尝试销毁进程，以防未正确关闭
            }
        }
    }

    /**
     * 根据模型文件获取分类标签
     * 拟合模型
     * @param scriptName
     * @return
     */
    public static String executePythonScript(String scriptName) {
        List<String> command = new ArrayList<>();
        command.add("python.exe");
        command.add(getPythonPath(scriptName)); // 脚本名

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                int exitCode = process.waitFor();
                if (exitCode != 0) {
                    throw new RuntimeException("Python脚本退出代码" + exitCode);
                }
                return reader.readLine();
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException("执行失败", e);
        } finally {
            if (process != null && process.isAlive()) {
                process.destroy(); // 尝试销毁进程，以防未正确关闭
            }
        }
    }
}
